﻿/**
*    @file:    qstringutils.cpp
*    @brief:
*    @author:  sanshi522@163.com
*    @date:    2025-01-05  23:57
*/
#include "qstringutils.h"
#include "math.h"

// 将字符串转为大写
QString QStringUtils::toUpperCase(const QString &str)
{
    return str.toUpper();
}

// 将字符串转为小写
QString QStringUtils::toLowerCase(const QString &str)
{
    return str.toLower();
}

// 替换字符串中的子串
QString QStringUtils::replace(const QString &str, const QString &before, const QString &after)
{
    QString resStr = str;
    return resStr.replace(before, after);
}

// 使用正则表达式替换字符串中的子串
QString QStringUtils::replaceRegex(const QString &str, const QRegularExpression &regex, const QString &replacement)
{
    QString resStr = str;
    return resStr.replace(regex, replacement);
}

// 分割字符串
QStringList QStringUtils::split(const QString &str, const QString &sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs)
{
    return str.split(sep, behavior, cs);
}

// 连接字符串列表
QString QStringUtils::join(const QStringList &list, const QString &separator)
{
    return list.join(separator);
}

// 去除字符串两端的空白字符
QString QStringUtils::trimmed(const QString &str)
{
    return str.trimmed();
}

// 检查字符串是否为空（包括只包含空白的情况）
bool QStringUtils::isEmptyOrWhitespace(const QString &str)
{
    return str.trimmed().isEmpty();
}

// 获取字符串长度
int QStringUtils::length(const QString &str)
{
    return str.length();
}

// 判断字符串是否以指定前缀开头
bool QStringUtils::startsWith(const QString &str, const QString &prefix, Qt::CaseSensitivity cs)
{
    return str.startsWith(prefix, cs);
}

// 判断字符串是否以指定后缀结尾
bool QStringUtils::endsWith(const QString &str, const QString &suffix, Qt::CaseSensitivity cs)
{
    return str.endsWith(suffix, cs);
}

QString QStringUtils::formatBySecond(uint64_t m_second)
{
    QString timeStr;
    uint64_t day = floor(m_second / 86400);
    uint64_t hour = floor((m_second % 86400) / 3600);
    uint64_t minute = floor(m_second % 86400 % 3600 / 60);
    uint64_t second = floor(m_second % 86400 % 3600 % 60);

    if (day > 0)
    {
        timeStr = QString("%1天%2时%3分").arg(day).arg(hour).arg(minute);
    }
    else
    {
        timeStr = QString("%1时%2分%3秒").arg(hour).arg(minute).arg(second);
    }

    return timeStr;
}

QString QStringUtils::formatbyteSize(uint64_t size)
{
    QString strUnit;
    double dSize = size * 1.0;
    if (dSize <= 0)
    {
        dSize = 0.0;
    }
    else if (dSize < 1024)
    {
        strUnit = "Bytes";
    }
    else if (dSize < 1024 * 1024)
    {
        dSize /= 1024;
        strUnit = "KB";
    }
    else if (dSize < 1024 * 1024 * 1024)
    {
        dSize /= (1024 * 1024);
        strUnit = "MB";
    }
    else if (dSize < 1024 * 1024 * 1024 * 1024.0)
    {
        dSize /= (1024 * 1024 * 1024);
        strUnit = "GB";
    }
    else
    {
        dSize /= (1024 * 1024 * 1024 * 1024.0);
        strUnit = "TB";
    }
    return QString("%1 %2").arg(QString::number(dSize, 'f', 2)).arg(strUnit);
}

QString QStringUtils::numberDivision(double value, int interval, int precision)
{
    QString strVal = QString::number(value, 'f', precision);
    QString outStr = "";
    int num = 1;
    int startPos = strVal.length() - 1;
    if (strVal.lastIndexOf('.') != -1)
    {
        outStr = strVal.right(strVal.length() - strVal.lastIndexOf('.'));
        startPos = strVal.lastIndexOf('.') - 1;
    }
    for (int pos = startPos; pos >= 0; pos--)
    {
        outStr.insert(0, strVal.at(pos));
        if (num % interval == 0 & pos != 0)
        {
            outStr.insert(0, ',');
        }
        num++;
    }
    return outStr;
}
